Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
¿Por qué la palabra clave "esto" en esta función no da un error en JavaScript por duplicación?

 function makeUser() { return { name: "John", ref() { return this; } }; } let user = makeUser(); alert(user.ref().name);

Por lo que aprendí de este tutorial , se definen como el objeto antes del punto y dado que este es el valor de retorno de la función, pensé que sería el usuario.

Y este tutorial lo define como el objeto que ejecuta la función actual.

Creo que el objeto que ejecuta la función actual es el usuario. Cuando reemplaza esto por su valor, se convierte en alert(user.user.name); .

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

El valor de "esto" no se refiere al nombre de un objeto, se refiere al objeto mismo . Entonces no hay "duplicación" aquí, solo está devolviendo un objeto, que resulta ser el mismo objeto con el que comenzó.

Esto es quizás más claro si separas las cosas:

 function makeUser() { // Create an object let myObject = { name: "John" }; // Create a function which returns that object let myFunction = function(){ return myObject; }; // Store the function on the object myObject.ref = myFunction; // Return the new object return myObject; } // Create an object let user = makeUser(); // Get the name from the object alert(user.name); // Call the ref() function, which happens to return the same object let otherUser = user.ref(); // Get the name again alert(otherUser.name); // No matter how many times you call .ref() it will return the same object alert(user.ref().ref().ref().ref().name);

about 4 years ago · Juan Pablo Isaza Report

0

esto se refiere a un objeto al que pertenece la función función o al objeto ventana si no pertenece a ningún objeto. en este caso, la función ref () devuelve el objeto propietario, por ejemplo,

cuando llames a ref() volverá así

 { name: 'John', ref: [Function: ref] }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!